package com.sskj.givecoin.v.activity;

import android.content.Intent;
import android.graphics.Typeface;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.widget.Toolbar;
import android.text.InputFilter;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.sskj.givecoin.MyAppLication;
import com.sskj.givecoin.R;
import com.sskj.givecoin.common.App;
import com.sskj.givecoin.common.Constants;
import com.sskj.givecoin.common.MoneyValueFilter;
import com.sskj.givecoin.m.bean.CoinFeeBean;
import com.sskj.givecoin.p.activity.WithdrawPresenter;
import com.sskj.givecoin.utils.CheckUtil;
import com.sskj.givecoin.utils.ClickUtil;
import com.sskj.givecoin.utils.ToastUtil;
import com.sskj.givecoin.v.base.BaseActivity;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subscribers.DisposableSubscriber;

/**
 * <pre>
 *     USDT提币
 *     author : 吕志豪
 *     e-mail : 1030753080@qq.com
 *     time   : 2018/04/03
 *     desc   :
 *     version: 1.0
 * </pre>
 */

public class WithdrawActivity extends BaseActivity<WithdrawPresenter> {

    @BindView(R.id.et_address)
    EditText etAddress;
    @BindView(R.id.iv_address)
    ImageView ivAddress;
    @BindView(R.id.et_number)
    EditText etNumber;
    @BindView(R.id.tv_all)
    TextView tvAll;
    @BindView(R.id.view_line)
    View viewLine;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.bt_send_code)
    TextView btSendCode;
    @BindView(R.id.et_check_code)
    EditText etCheckCode;
    @BindView(R.id.bt_withdraw)
    Button btWithdraw;
    @BindView(R.id.toolbar)
    public Toolbar mToolbar;
    @BindView(R.id.collapsing_toolbar)
    public CollapsingToolbarLayout mCollapsingToolbar;
    @BindView(R.id.tv_account)
    TextView mTvAccount;
    @BindView(R.id.tv_max_num)
    TextView mTvMaxNum;
    @BindView(R.id.tv_fee)
    TextView tvFee;
    @BindView(R.id.tvMoney)
    TextView tvMoney;
    @BindView(R.id.recharge_hint)
    TextView tvRechargeHint;
    @BindView(R.id.withdraw_unit)
    TextView tvWithdrawUnit;
    private DisposableSubscriber<Long> disposableSubscriber;
    private Double minCoin = 0d;
    private Double maxCoin = 0d;
    private Double wallone;
    private int mAssetId;
    private double mTb_fee;


    @Override
    protected int getLayoutResId() {
        return R.layout.activity_withdraw;
    }

    @Override
    public WithdrawPresenter getPresenter() {
        return new WithdrawPresenter();
    }

    @Override
    protected void onViewBinding() {
        mAssetId = getIntent().getIntExtra(Constants.INTENT_ASSET_ID, -1);
        String assetName = getIntent().getStringExtra(Constants.INTENT_ASSET_NAME);
        ClickUtil.click(btSendCode, () -> {
            if (CheckUtil.isEmail(MyAppLication.getMobile())) {
                mPresenter.sendEmail();
            } else {
                mPresenter.sendCode();
            }
        });

        ClickUtil.click(ivAddress, () -> {
            startActivityForResult(new Intent(this, CashAddressListActivity.class).putExtra(Constants.INTENT_CALL_BACK, true), Constants.CASH_ADDRESS);
        });
        Typeface font = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD);
        mCollapsingToolbar.setExpandedTitleTypeface(font);//加粗
        mToolbar.setTitle(App.INSTANCE.getString(R.string.strWithdrawActivity200));
        mToolbar.setNavigationIcon(R.mipmap.icon_back);
        mToolbar.setNavigationOnClickListener(v -> finish());

//        mTitle.setText(assetName+App.INSTANCE.getString(R.string.strWithdrawActivity200));
//        mToolBar.setLeftButtonOnClickLinster(v -> finish());
        tvRechargeHint.setText("提币须知： \n确认提币地址为" + assetName + App.INSTANCE.getString(R.string.strWithdrawActivity203) + assetName + App.INSTANCE.getString(R.string.strWithdrawActivity204));
        tvWithdrawUnit.setText(assetName);
        //        mToolBar.setRightButtonIcon(getResources().getDrawable(R.mipmap.icon_bill));
//        mToolBar.setRightButtonOnClickLinster(view -> startActivity(new Intent(mActivity, WithdrawRecordActivity.class)));
        ClickUtil.click(btWithdraw, () -> {
            if (TextUtils.isEmpty(etAddress.getText())) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.strWithdrawActivity205));
                return;
            }
            if (TextUtils.isEmpty(etNumber.getText())) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.strWithdrawActivity206));
                return;
            }
            Double aDouble = Double.valueOf(etNumber.getText().toString());
            if (aDouble < minCoin) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.strWithdrawActivity207) + minCoin + App.INSTANCE.getString(R.string.strWithdrawActivity208));
                return;
            }

            if (aDouble > maxCoin) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.strWithdrawActivity209) + maxCoin + App.INSTANCE.getString(R.string.strWithdrawActivity208));
                return;
            }

            if (aDouble > wallone - mTb_fee) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.strWithdrawActivity211));
                return;
            }
            if (TextUtils.isEmpty(etPassword.getText())) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.strWithdrawActivity212));
                return;
            }
            mPresenter.withdraw(etNumber.getText().toString().trim(),
                    etPassword.getText().toString().trim(),
                    etCheckCode.getText().toString().trim(),
                    etAddress.getText().toString().trim(), mAssetId);
        });
//        EditChangeUtil.onChange(etCheckCode, s -> {
//            if (s.length() == 6) {
//                mPresenter.checkCode(s.toString());
//            }
//        });
        etNumber.setFilters(new InputFilter[]{new MoneyValueFilter().setDigits(4)});
//        mPresenter.getUserInfo();
        mPresenter.getCoinFee(mAssetId);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK & requestCode == Constants.CASH_ADDRESS) {
            String address_url = data.getStringExtra("address_url");
            if (!TextUtils.isEmpty(address_url)) {
                etAddress.setText(address_url);
            }
        }
    }

    public void startCount() {
        btSendCode.setEnabled(false);
        disposableSubscriber = new DisposableSubscriber<Long>() {
            @Override
            public void onNext(Long aLong) {
                int time = (int) (60 - aLong);
                btSendCode.setText(time + App.INSTANCE.getString(R.string.strWithdrawActivity214));
            }

            @Override
            public void onError(Throwable t) {
                System.out.println(t.toString());
            }

            @Override
            public void onComplete() {
                btSendCode.setText(App.INSTANCE.getString(R.string.strWithdrawActivity215));
                btSendCode.setEnabled(true);
                if (!disposableSubscriber.isDisposed()) {
                    disposableSubscriber.dispose();
                    disposableSubscriber = null;
                }

            }
        };
        Flowable.interval(0, 1, TimeUnit.SECONDS, Schedulers.newThread())
                .take(60)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(disposableSubscriber);
    }

    public void setCodeOk() {
//        btWithdraw.setEnabled(true);
    }

    public void updateCoin(CoinFeeBean data) {
        wallone = Double.valueOf(data.getBalance());
        mTb_fee = data.getTb_fee();
        tvMoney.setText(wallone + "USDT");
        mTvMaxNum.setText(App.INSTANCE.getString(R.string.strWithdrawActivity216) + data.getBalance());
        ClickUtil.click(tvAll, () -> {
            etNumber.setText(String.valueOf(wallone - data.getTb_fee()));
        });
        minCoin = data.getTb_min();
        maxCoin = data.getTb_max();
        etNumber.setHint(App.INSTANCE.getString(R.string.strWithdrawActivity217) + data.getTb_min() + App.INSTANCE.getString(R.string.strWithdrawActivity218) + data.getTb_max());
        tvFee.setText(App.INSTANCE.getString(R.string.strWithdrawActivity219) + data.getTb_fee() + App.INSTANCE.getString(R.string.fabi_detai9));
    }
}
